package com.wgu.modules.service.impl;

import com.wgu.entity.BookInfo;
import com.wgu.entity.ChapterDetailInfo;
import com.wgu.entity.ChapterInfo;
import com.wgu.modules.repository.BookRepository;
import com.wgu.modules.repository.ChapterDetailRepository;
import com.wgu.modules.repository.ChapterRepository;
import com.wgu.modules.service.BookService;
import com.wgu.modules.service.ChapterService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author: w
 * @Date: 2019/7/4 17:29
 * @Version 1.0
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ChapterServiceImpl implements ChapterService {
    @Resource
    private ChapterRepository chapterRepository;
    @Resource
    private BookRepository bookRepository;
    @Resource
    private ChapterDetailRepository chapterDetailRepository;
    @Resource
    private BookService bookService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void export(String bookName) {
        BookInfo bookInfo = bookService.getBookByName(bookName);
        List<Map<String,Object>> mapList = chapterRepository.findAllQDChapterByBookName(bookName);
        for(int i=0;i<mapList.size();i++){
            String title = (String) mapList.get(i).get("title");
            String shangjia = (String) mapList.get(i).get("shangjia");
            // 上传时间暂不用管理
            //String shangchuan = (String) mapList.get(i).get("shangchuan");
            String zishu = (String) mapList.get(i).get("zishu");
            String content = (String)mapList.get(i).get("content");

            if(StringUtils.isNotBlank(shangjia)){
                bookInfo.setShelfDate(shangjia);
                bookRepository.save(bookInfo);
            }

            ChapterInfo chapterInfo = chapterRepository.findByBookIdAndTitle(bookInfo.getId(),title).orElse(new ChapterInfo());
            chapterInfo.setBookId(bookInfo.getId());
            chapterInfo.setTitle(title);
            chapterInfo.setWordNum(zishu);
            //除了第一章 设置上一章
            if(i!=0){
                Long prevId=chapterRepository.findMaxIdByBookId(bookInfo.getId());
                chapterInfo.setPrevId(prevId);
            }
            chapterRepository.save(chapterInfo);
            ChapterDetailInfo chapterDetailInfo = chapterDetailRepository.findByChapterId(chapterInfo.getId()).orElse(new ChapterDetailInfo());
            chapterDetailInfo.setChapterId(chapterInfo.getId());
            chapterDetailInfo.setContent(content);
            chapterDetailRepository.save(chapterDetailInfo);
        }
        // 整理下一章
        List<ChapterInfo> list = chapterRepository.findByBookId(bookInfo.getId());
        for(int i = 0;i<list.size();i++){
            if(i!=list.size()-1){
                Long nextId = list.get(i+1).getId();
                list.get(i).setNextId(nextId);
                chapterRepository.save(list.get(i));
            }
        }
    }
}
